var log = require('../../log.js')

let title = ''
let singer = ''
Page({
  data: {
    list: [],
    loadModal: false,
    loading: true
  },
  onLoad() {
    this.getList()
  },
  //点赞
  likes(e) {
    if (this.data.list[e.currentTarget.dataset.index].likes == null) {
      var likes = 0
    } else {
      var likes = this.data.list[e.currentTarget.dataset.index].likes
    }
    var i = "list[" + e.currentTarget.dataset.index + "].likes"
    this.setData({
      [i]: likes + 1
    })
    var value = wx.getStorageSync('likes')
    for (var a = 0; a <= value.length - 1; a++) {
      if (value[a].id == e.currentTarget.dataset.id) {
        console.log('相等')
        value[a].likes = value[a].likes + 1
        wx.setStorageSync('likes', value)
        return
      }
    }
    console.log('不相等')
    var data = [{
      id: e.currentTarget.dataset.id,
      likes: likes + 1
    }]
    if (value.length == 0) {
      wx.setStorageSync('likes', data)
    } else {
      wx.setStorageSync('likes', data.concat(value))
    }
  },
  likesUpdate() {
    var value = wx.getStorageSync('likes')
    if (value != '') {
      wx.cloud.callFunction({
          name: 'likesUpdate',
          data: {
            value: value
          }
        })
        .then(res => {
          console.log(res)
        })
        .catch(err => {
          console.log(err)
        })
      try {
        wx.removeStorageSync('likes')
      } catch (e) {}
    } else {
      console.log('无点赞记录')
      return
    }
  },
  onHide() {
    console.log('********onHide********')
    this.likesUpdate()
  },
  //获取列表数据
  getList() {
    this.loadModal()
    let len = this.data.list.length
    wx.cloud.callFunction({
        name: 'getSongList',
        data: {
          len: len,
          pageNum: 50
        }
      })
      .then(res => {
        console.log(res.result.data)
        let dataList = res.result.data
        if (dataList.length <= 0) {
          wx.showToast({
            icon: 'none',
            title: '没有更多数据啦'
          })
        }
        this.setData({
          list: this.data.list.concat(dataList),
          loadModal: false,
          loading: false
        })
        wx.stopPullDownRefresh()
      })
      .catch(err => {
        log.error(err)
        this.setData({
          loadModal: false
        })
      })
  },
  onReachBottom() {
    this.getList()
  },
  //跳转详情页
  goDetail(e) {
    console.log('点击了跳转点歌详情', e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '/pages/songListDetail/songListDetail?id=' + e.currentTarget.dataset.id
    })
  },
  gosongOrder() {
    wx.navigateTo({
      url: '/pages/songOrder/songOrder'
    })
  },
  loadModal() {
    this.setData({
      loadModal: true
    })
    var timerTem = setTimeout(() => {
      this.setData({
        loadModal: false
      })
    }, 700)
    var that = this
    that.setData({
      timer: timerTem
    })
  },
  onUnload() {
    console.log("+++++++++onUnload++++++++++")
    clearInterval(this.data.timer) //销毁定时器
    this.likesUpdate()
  },
  onShareAppMessage: function (res) {
    const pages = getCurrentPages()
    const currentPage = pages[pages.length - 1]
    const url = `/${currentPage.route}`
    return {
      title: 'TLradio - 点歌列表',
      path: url,
      //imageUrl: '../../images/share1.png'
    }
  },
  onShareTimeline: function () {
    return {
      title: 'TLradio - 点歌列表',
      query: 'from=pyq'
    }
  }
})